import { Component, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OutsourcingWorkFlowServiceProxy, WorkflowServiceProxy, OutsouringServiceProxy, UserServiceProxy, OutsourcingViewDto, DeliveryServiceProxy, ProcessingOrderServiceProxy } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
import { FlowworkauditDetailModelComponent } from '../flowworkauditDetailModel/flowworkauditDetailModel.component';
import { ProcessingOrderListComponent } from '../ProcessingOrderList/ProcessingOrderList.component';
import { OutsourcingApplyChangeListComponent } from '../OutsourcingApplyChangeList/OutsourcingApplyChangeList.component';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { ResourceLoader } from '@angular/compiler';

import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';
import * as moment from 'moment';


@Component({
    selector: 'app-taskoutsouring',
    templateUrl: './taskoutsouring.component.html',
    styleUrls: ['./taskoutsouring.component.css']
})
export class TaskoutsouringComponent extends AppComponentBase {
    @ViewChild('flowworkauditdetailmodel') flowworkauditdetailmodel: FlowworkauditDetailModelComponent;
    @ViewChild('processingOrderList') processingOrderList: ProcessingOrderListComponent;
    @ViewChild('OutsourcingApplyChangeList') OutsourcingApplyChangeList: OutsourcingApplyChangeListComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    filterText = '';
    startDate: any;
    endDate: any;
    advancedFiltersAreShown = false;
    item: any;
    outsourcings: OutsourcingViewDto[];
    constructor(injector: Injector,
        private _outsourcingWorkFlowServiceProxy: OutsourcingWorkFlowServiceProxy,
        private _userServiceProxy: UserServiceProxy,
        private _outsourcingServiceProxy: OutsouringServiceProxy,
        private _processOrderServiceProxy: ProcessingOrderServiceProxy,
        private _deliveryServiceProxy: DeliveryServiceProxy,
        private _filedownloadService: FileDownloadService,
        private _workflowServiceProxy: WorkflowServiceProxy) {

        super(injector);
    }

    getOutsourcingAgreedList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.primengTableHelper.showLoadingIndicator();

        this._outsourcingServiceProxy.getOutsourcingForList(
            this.startDate,
            this.endDate,
            this.filterText,
            'code',
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event),
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;

            this.outsourcings = result.items;
            this.primengTableHelper.hideLoadingIndicator();

            _.forEach(result.items, viewItem => {
                console.log(viewItem);
                let currentD = this.outsourcings.find((element) => (element.id === viewItem.id));
                if (viewItem.createdUser !== '' && viewItem.createdUser !== '') {
                    this._userServiceProxy.getUserInfo(viewItem.createdUser)
                        .subscribe(userResult => {
                            currentD.createdUserName = userResult.surname + userResult.name;
                        });
                } else {
                    currentD.createdUser = '-';
                    currentD.createdUserName = '-';
                }

                this._processOrderServiceProxy.getProcessingAmountForTaskId(viewItem.taskForModelId, viewItem.code).subscribe(result => {
                    currentD.processingAmount = result.toString();
                });

                this._deliveryServiceProxy.getDeliveryAmountWorkFlowComplete(viewItem.taskForModelId, viewItem.code).subscribe(result => {
                    currentD.deliveryAmount = result.toString();
                });


            });
        });
    }
    Detail(records): void {
        let workflowName = records.outsourcingType === 0 ? '一般过程外协申请' : '关键过程外协申请';

        this.flowworkauditdetailmodel.show(workflowName, records.workflowId);
    }
    OpenProcesingList(code: string): void {
        this.processingOrderList.show(code);
    }
    OpenOutsourcingApplyChangeList(code: string, outsourcingType: number): void {
        this.OutsourcingApplyChangeList.show(code, outsourcingType);
    }
    exportToexcel(event?: LazyLoadEvent): void {
        this._outsourcingServiceProxy.getOutsourcingForListToExecl(
            this.filterText,
            this.startDate,
            this.endDate
            ).subscribe(dto => {
            this._filedownloadService.downloadTempFile(dto);
        });
    }
}
